/**
 * @Title: JSplugin.java
 * @Package com.sanqi.android.sdk.apinterface
 * Copyright: Copyright (c) 2013
 * @author xiaoming.yuan
 * @date 2013-10-9 上午11:20:57
 * @version V1.0
 */

package com.zygame.sdk.view;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.zygame.sdk.CmCode;
import com.zygame.sdk.ZyGameSDK;
import com.zygame.sdk.bean.PayResult;
import com.zygame.sdk.bean.UserBean;
import com.zygame.sdk.db.UserDao;
import com.zygame.sdk.model.UserModel;
import com.zygame.sdk.utils.SharedPreferencesHelper;
import com.zygame.sdk.utils.toast.ToastUtil;

/**
 * @ClassName: JSplugin
 * @author xiaoming.yuan
 * @date 2013-10-9 上午11:20:57
 */

@SuppressLint("HandlerLeak")
public class JSplugin {

	// private static final String TAG = "JSplugin";

	// public static final String ANDROIDJSPLUG = "androidJSPlug";

	// private static final int SUCCESS_BACK_RECHARGE = 1;

	// private static final int ERROR_BACK_RECHARGE = 2;

	// private static final int ALIPAY_RECHARGE = 3;

	// private static final int UNIONPAY_RECHARGE = 4;

	// private static final int CALL_PHONE = 5;

	// private ProgressDialog mProgress = null;

	private Activity mActivity;

	public static String sMoney;
	private Handler handler;

	public JSplugin(Activity activity) {
		this.mActivity = activity;
		handler = new Handler();

	}

	/**
	 * @author K
	 * @Description: TODO(js修密码回调)
	 * @date 2014-12-13 下午5:22:13
	 */
	@JavascriptInterface
	public void jsChangePassWordEvent(final String passWord) {
		// SharedPreferencesHelper helper =
		// SharedPreferencesHelper.getInstance();
		// boolean flag = helper.savePassWord(mActivity, passWord);
	}

	/**
	 * @author K
	 * @Description: TODO(js65币充值回调)
	 * @date 2014-12-13 下午5:21:31
	 * @param number
	 */
	@JavascriptInterface
	public void js65RechargeSuccess(final String number) {
		handler.post(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (!TextUtils.isEmpty(number)) {
					UserModel.getInstance().setPursecoin(number);
				}
			}
		});
	};

	/**
	 * 
	 * @author K
	 * @Description: TODO(js复制验证码回调)
	 * @date 2014-12-13 下午5:46:04
	 * @param code
	 */
	@JavascriptInterface
	@SuppressLint("NewApi")
	public void jsCopyVerificationCallBack(String code) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			if (TextUtils.isEmpty(code)) {
				ToastUtil.showToast(mActivity, "复制失败,请手动输入");
			} else {
				ClipboardManager cmb = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
				cmb.setText(code);
				ToastUtil.showToast(mActivity, "复制成功");

			}
		} else {
			if (TextUtils.isEmpty(code)) {
				ToastUtil.showToast(mActivity, "复制失败,请手动输入");
			} else {
				android.text.ClipboardManager cmb1 = (android.text.ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
				cmb1.setText(code);
				ToastUtil.showToast(mActivity, "复制成功");

			}
		}
	}

	/**
	 * @author K
	 * @Description: TODO(返回商店)
	 * @date 2014-12-18 下午2:48:02
	 */
	@JavascriptInterface
	public void goBackStoreCallBack() {
		rechargeEndFinshActivity();
	}

	@JavascriptInterface
	public void gameRecharge(final String msg) {
		handler.post(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				ToastUtil.showToast(mActivity, msg);
				mActivity.finish();
			}
		});

	}

	@JavascriptInterface
	public void refreshPursecoin(String param) {
		UserModel.getInstance().setPursecoin(param);
	}

	@JavascriptInterface
	public void getPhoneNum(String param) {
		Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + param.trim()));
		mActivity.startActivity(intent);
	}

	@JavascriptInterface
	public void rechargeEndFinshActivity() {
		ZyGameSDK.getInstance().onPayResult(null);
		mActivity.finish();
	}

	/**
	 * @author K
	 * @Description: TODO(js绑定手机回调)
	 * @date 2014-12-13 下午5:22:13
	 * @param phone
	 */
	@JavascriptInterface
	public void jsBinderPhoneCallBack(final String phone) {
		handler.post(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (!TextUtils.isEmpty(phone)) {
					UserModel.getInstance().setPhone_bind(1);
				}
			}
		});

	};

	/**
	 * 支付成功回调
	 * 
	 * String flag 成功设置 1 ；失败设置 0
	 */
	@JavascriptInterface
	public void payMoneyCallback(String flag) {
		PayResult payResult = new PayResult();
		if (flag.equals("1")) {
			payResult.setResultCode(CmCode.CODE_PAY_SUCCESS);
			payResult.setExtension("pay success !");
		} else {
			payResult.setResultCode(CmCode.CODE_PAY_FAIL);
			payResult.setExtension("pay fail !");
		}
		ZyGameSDK.getInstance().onPayResult(payResult);
	}

	/**
	 * 支付宝支付
	 */
	@JavascriptInterface
	public void alipay(String param) {
//		new Alipay().alipay(param);
	}

	/**
	 * weichat支付
	 */
	@JavascriptInterface
	public void weichatPay(String param) {
//		new WechatPay().weichatPay(param);
	}

	/**
	 * 支付
	 */
	@JavascriptInterface
	public void pay(String params) {
//		String flag = null;
//		try {
//			flag = new JSONObject(params).getJSONObject("info").optString("return_payway");
//			ParsePayParams.whichPay = flag;
//		} catch (JSONException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		if (flag.equals("mobalipay")) { // alipay
//			new Alipay().alipay(params);
//		} else if (flag.equals("androidwechatpay")) { // weichat pay
//			new WechatPay().weichatPay(params);
//		} else if (flag.equals("unionpay")) { // union pay
//			new Unionpay().unionpay(params);
//		}
	}

	// -------------------以下是新版js回调-----------------------------

	/**
	 * 切换账号
	 */
	@JavascriptInterface
	public void jsSwitchAccount() {
//		new Login(CmGameSDK.getInstance().getContext()).show();
//		CmGameSDK.getInstance().setOnSwitchAccountFlag(true);
//		mActivity.finish();
	}

	/**
	 * 关闭页面
	 */
	@JavascriptInterface
	public void jsCloseWeb() {
		mActivity.finish();
	}
	
	/**
	 * 关闭充值页面
	 */
	@JavascriptInterface
	public void close(String str){
		PayResult payResult = new PayResult();
		payResult.setResultCode(CmCode.CODE_PAY_SUCCESS);
		payResult.setExtension("alipay success !");
		ZyGameSDK.getInstance().onPayResult(payResult);
		mActivity.finish();
	}

	/**
	 * 
	 * 消息提示
	 * 
	 * @param allMsg
	 *            所有消息
	 *            ＝ allMsg - 1 未读消息 每调一次回调数量就减一
	 */
	@JavascriptInterface
	public void jsMsgPrompt(String allMsg) {
		System.out.println("消息总数：" + allMsg);
		SharedPreferencesHelper.getInstance().setMsgPromptAmount(mActivity, Integer.parseInt(allMsg.trim()) - 1);
	}

	/**
	 * @Description: TODO(js绑定手机回调)
	 */
	@JavascriptInterface
	public void jsBinderPhone(final String phoneNmu) {
		// handler.post(new Runnable() {
		// @Override
		// public void run() {
		// // TODO Auto-generated method stub
		// if (!TextUtils.isEmpty(phone)) {
		// UserModel.getInstance().setPhone_bind(1);
		// }
		// }
		// });
	}

	/**
	 * 修改密码
	 */
	@JavascriptInterface
	public void jsModifyPwd(String password) {
		
		System.out.println("password = " + password);
		
		String account = UserModel.getInstance().getLogin_account();
		if (account != null) {
			UserBean ub = new UserBean(account, password);
			new UserDao().addToDb(ub);
		}
		ToastUtil.showToast(ZyGameSDK.getInstance().getContext(), "修改密码成功 !");
	}

	/**
	 * 
	 * @Description: TODO(js复制领取礼包码)
	 */
	@JavascriptInterface
	@SuppressLint("NewApi")
	public void jsCopyGiftCode(String giftCode) {

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			if (TextUtils.isEmpty(giftCode)) {
				ToastUtil.showToast(mActivity, "复制失败,请手动输入");
			} else {
				ClipboardManager cmb = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
				cmb.setText(giftCode);
				ToastUtil.showToast(mActivity, "复制成功");

			}
		} else {
			if (TextUtils.isEmpty(giftCode)) {
				ToastUtil.showToast(mActivity, "复制失败,请手动输入");
			} else {
				android.text.ClipboardManager cmb1 = (android.text.ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
				cmb1.setText(giftCode);
				ToastUtil.showToast(mActivity, "复制成功");

			}
		}
	}

}
